package com.jiezhuo.controller.jw;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.cmeduSchool.JwXqNjXj;
import com.jiezhuo.entity.cmeduSchool.TJzg;
import com.jiezhuo.entity.jw.DmXk;
import com.jiezhuo.entity.jw.JwVo;
import com.jiezhuo.entity.jw.StuVo;
import com.jiezhuo.mapper.center.JwXqStudentMapper;
import com.jiezhuo.service.IJwXqNjXjService;
import com.jiezhuo.service.jw.DmXkService;
import com.jiezhuo.service.jw.JwUserlistService;

import com.jiezhuo.service.jzg.TJzgService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "教务-管理员")
@RestController
@RequestMapping("jw-admin-management")
public class AdministratorController {
    @Autowired
    JwXqStudentMapper jwXqStudentMapper;

    @Autowired
    IJwXqNjXjService jwXqNjXjService;
    @Autowired
    DmXkService dmXkService;

    @Autowired
    JwUserlistService jwUserlistService;
    @Autowired
    TJzgService tJzgService;
    @ApiOperation("获取学段")
    @GetMapping("getPhase")
    public R getPhase(){
       ArrayList<HashMap<String,Object>> arrayList= jwXqStudentMapper.getPhase();
        return R.ok().data("list",arrayList);
    }
    @ApiOperation("获取学期学段")
    @GetMapping("getXqPhase")
    public R getXqPhase(JwVo jwVo){
        ArrayList<HashMap<String,Object>> arrayList= jwXqStudentMapper.getXqPhase(jwVo);
        return R.ok().data("list",arrayList);
    }

    @ApiOperation("获取学科字段分类")
    @GetMapping("getXqPhaseClassify")
    public R getXqPhaseClassify(){
        ArrayList<HashMap<String,Object>> arrayList= jwXqStudentMapper.getXqPhaseClassify();
        return R.ok().data("list",arrayList);
    }
    @ApiOperation("删除年级学科")
    @GetMapping("deleteNjXq")
    public R deleteNjXq(JwVo jwVo){
        QueryWrapper<JwXqNjXj> qw = new QueryWrapper<>();
        qw.eq("ID",jwVo.getId());
        int delete = jwXqNjXjService.getBaseMapper().delete(qw);
        return delete>0?R.ok():R.err();
    }
    @ApiOperation("新增修改年级学科")
    @PostMapping("saveOrUpdateSubject")
    public R saveOrUpdateSubject(@RequestBody DmXk dmXk){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        QueryWrapper<TJzg> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("teacherid",userId);
        TJzg tJzg = tJzgService.getBaseMapper().selectOne(objectQueryWrapper);
        String gzXqdm = tJzg.getGzXqdm();
        dmXk.setGlXqdm(gzXqdm);
        boolean delete=true;
        if(StringUtils.isEmpty(dmXk.getId())){
             delete = dmXkService.save(dmXk);
        }else {
            int i = dmXkService.getBaseMapper().updateById(dmXk);
        }
        return delete?R.ok():R.err();
    }
    @ApiOperation("搜索教师所教班级")
    @GetMapping("getTeacherRk")
    public R getTeacherRk( JwVo jwVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }else {
                jwVo.setGzXqdm("'" + jwVo.getGzXqdm() + "'");
            }
        }
     R r = dmXkService.getTeacherRk(jwVo);
        return r;
    }
    @ApiOperation("搜索学校班级")
    @GetMapping("getSchoolClassInfo")
    public R getSchoolClassInfo( JwVo jwVo){
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())||ObjectUtils.isEmpty(jwVo.getXq())){
            return  R.ok().msg("请选择学校或学期");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }else {
                jwVo.setGzXqdm("'" + jwVo.getGzXqdm() + "'");
            }
        }
        R r = dmXkService.getSchoolClassInfo(jwVo);
        return r;
    }

    @ApiOperation("搜索学校走班班级")
    @GetMapping("getSchoolMoveClassInfo")
    public R getSchoolMoveClassInfo( JwVo jwVo){
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())||ObjectUtils.isEmpty(jwVo.getXq())){
            return  R.ok().msg("请选择学校或学期");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }else {
                jwVo.setGzXqdm("'" + jwVo.getGzXqdm() + "'");
            }
        }
        R r = dmXkService.getSchoolMoveClassInfo(jwVo);
        return r;
    }

    @ApiOperation("获取小升初数据")
    @GetMapping("getPrimaryUpgrade")
    public R getPrimaryUpgrade( JwVo jwVo){
        if(ObjectUtils.isEmpty(jwVo.getXq())){
            return  R.ok().msg("请选择学校或学期");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }else {
                jwVo.setGzXqdm("'" + jwVo.getGzXqdm() + "'");
            }
        }
        R r = dmXkService.getPrimaryUpgrade(jwVo);
        return r;
    }
    @ApiOperation("毕业档案详情")
    @GetMapping("getByInfo")
    public R getByInfo( StuVo s){
        if(ObjectUtils.isEmpty(s.getXq())){
            return  R.ok().msg("请选择学校或学期");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        s.setStuId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                s.setXqdm("'not'");
            }else {
                s.setXqdm("'" + s.getXqdm() + "'");
            }
        }
         R r = dmXkService.getByInfo(s);
        return r;
    }


}
